package com.xx.common.utils;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import org.slf4j.MDC;

public class TraceIdUtil {

    public static final String TRACE_ID_KEY = "TraceId";

    public static String generateTraceId() {
        String traceId = IdUtil.fastSimpleUUID().toUpperCase();
        MDC.put(TRACE_ID_KEY, traceId);
        return traceId;
    }

    public static String generateTraceId(String traceId) {
        if (StrUtil.isBlank(traceId)) {
            return generateTraceId();
        }
        MDC.put(TRACE_ID_KEY, traceId);
        return traceId;
    }

    /**
     * 获取链路Id
     */
    public static String getTraceId() {
        return MDC.get(TRACE_ID_KEY);
    }

    public static void removeTraceId() {
        MDC.remove(TRACE_ID_KEY);
    }

}
